package com.kfyty.auto.host.utils;

import com.kfyty.loveqq.framework.core.exception.ResolvableException;
import com.kfyty.loveqq.framework.core.utils.CommandUtil;

import java.io.File;
import java.util.concurrent.atomic.AtomicBoolean;

public class Cmd {

    public static File copy(File file, File target) {
        AtomicBoolean result = new AtomicBoolean(false);

        CommandUtil.execute(e -> result.compareAndSet(false, e.contains("已复制")), null, "gbk", new String[]{"cmd", "/c", "copy", file.getAbsolutePath(), target.getAbsolutePath()});

        if (!result.get()) {
            throw new ResolvableException("copy file failed.");
        }

        return target.isFile() ? target : new File(target.getAbsolutePath(), file.getName());
    }

    public static boolean ping(String ip) {
        AtomicBoolean result = new AtomicBoolean(false);

        CommandUtil.execute(e -> result.compareAndSet(false, e.contains("(0% 丢失)")), null, "gbk", new String[]{"cmd", "/c", "ping", ip});

        return result.get();
    }

    public static void flushDNS() {
        AtomicBoolean result = new AtomicBoolean(false);

        CommandUtil.execute(e -> result.compareAndSet(false, e.contains("已成功刷新")), null, "gbk", new String[]{"cmd", "/c", "ipconfig/flushdns"});

        if (!result.get()) {
            throw new ResolvableException("flush dns failed.");
        }
    }
}
